package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PayDictItem;
import com.jeequan.jeepay.service.PayDictItemService;
import com.jeequan.jeepay.service.mapper.PayDictItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PayDictItem;
//import .mapper.PayDictItemMapper;
//import .service.PayDictItemService;
 /**
 * 数据字典元素表;(pay_dict_item)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PayDictItemServiceImpl implements PayDictItemService {
    @Autowired
    private PayDictItemMapper payDictItemMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PayDictItem queryById(Integer id){
        return payDictItemMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param payDictItem 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PayDictItem> paginQuery(PayDictItem payDictItem, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PayDictItem> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(payDictItem.getGroupCode())){
            queryWrapper.eq(PayDictItem::getGroupCode, payDictItem.getGroupCode());
        }
        if(StrUtil.isNotBlank(payDictItem.getItemCode())){
            queryWrapper.eq(PayDictItem::getItemCode, payDictItem.getItemCode());
        }
        if(StrUtil.isNotBlank(payDictItem.getItemName())){
            queryWrapper.eq(PayDictItem::getItemName, payDictItem.getItemName());
        }
        if(StrUtil.isNotBlank(payDictItem.getRemark())){
            queryWrapper.eq(PayDictItem::getRemark, payDictItem.getRemark());
        }
        if(StrUtil.isNotBlank(payDictItem.getCreatedBy())){
            queryWrapper.eq(PayDictItem::getCreatedBy, payDictItem.getCreatedBy());
        }
        if(StrUtil.isNotBlank(payDictItem.getUpdatedBy())){
            queryWrapper.eq(PayDictItem::getUpdatedBy, payDictItem.getUpdatedBy());
        }
        //2. 执行分页查询
        Page<PayDictItem> pagin = new Page<>(current , size , true);
        IPage<PayDictItem> selectResult = payDictItemMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param payDictItem 实例对象
     * @return 实例对象
     */
    public PayDictItem insert(PayDictItem payDictItem){
        payDictItemMapper.insert(payDictItem);
        return payDictItem;
    }
    
    /** 
     * 更新数据
     *
     * @param payDictItem 实例对象
     * @return 实例对象
     */
    public PayDictItem update(PayDictItem payDictItem){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PayDictItem> chainWrapper = new LambdaUpdateChainWrapper<PayDictItem>(payDictItemMapper);
        if(StrUtil.isNotBlank(payDictItem.getGroupCode())){
            chainWrapper.set(PayDictItem::getGroupCode, payDictItem.getGroupCode());
        }
        if(StrUtil.isNotBlank(payDictItem.getItemCode())){
            chainWrapper.set(PayDictItem::getItemCode, payDictItem.getItemCode());
        }
        if(StrUtil.isNotBlank(payDictItem.getItemName())){
            chainWrapper.set(PayDictItem::getItemName, payDictItem.getItemName());
        }
        if(StrUtil.isNotBlank(payDictItem.getRemark())){
            chainWrapper.set(PayDictItem::getRemark, payDictItem.getRemark());
        }
        if(StrUtil.isNotBlank(payDictItem.getCreatedBy())){
            chainWrapper.set(PayDictItem::getCreatedBy, payDictItem.getCreatedBy());
        }
        if(StrUtil.isNotBlank(payDictItem.getUpdatedBy())){
            chainWrapper.set(PayDictItem::getUpdatedBy, payDictItem.getUpdatedBy());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PayDictItem::getId, payDictItem.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(payDictItem.getId());
        }else{
            return payDictItem;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = payDictItemMapper.deleteById(id);
        return total > 0;
    }
}